package com.pactera.madp.cp.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.clientLossManagement.RemindCommunicationAddDTO;
import com.pactera.madp.cp.api.dto.clientLossManagement.RemindCommunicationUpdateDTO;
import com.pactera.madp.cp.api.entity.clientLossManagement.RemindCommunicationEntity;
import com.pactera.madp.cp.api.vo.contact.ContactTagGroupIndexVO;
import com.pactera.madp.cp.api.vo.remind.QueryCommunicationVO;
import com.pactera.madp.cp.api.vo.remind.RemindCommunicationAddVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.clientLossManagement.RemindCommunicationMapper;
import com.pactera.madp.cp.service.EmployeeTagService;
import com.pactera.madp.cp.service.IWorkContactTagGroupService;
import com.pactera.madp.cp.service.RemindCommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @date: 2021/7/27 17:25
 * @description:
 */
@Service
public class RemindCommunicationServiceImpl extends ServiceImpl<RemindCommunicationMapper, RemindCommunicationEntity> implements RemindCommunicationService {

    @Autowired
    RemindCommunicationService remindCommunicationService;
    @Autowired
    IWorkContactTagGroupService workContactTagGroupService;
    @Autowired
    EmployeeTagService employeeTagService;

    @Override
    public R getAllTag() {
        List<ContactTagGroupIndexVO> companyTagList = workContactTagGroupService.getIndexList();
        RemindCommunicationAddVO remindCommunicationAddVO = new RemindCommunicationAddVO();
        remindCommunicationAddVO.setTagIds(companyTagList);
        return R.ok(remindCommunicationAddVO);
    }

    @Override
    public R setRule(RemindCommunicationAddDTO remindCommunicationAddDTO) {
        RemindCommunicationEntity entity = new RemindCommunicationEntity();
        String tagListStr = JSONUtil.toJsonPrettyStr(remindCommunicationAddDTO.getTagIds());
        entity.setTagIds(tagListStr);
        if(remindCommunicationAddDTO.getTime().equals(0)){
            throw new CheckedException("时长不能设置为0天!");
        }
        entity.setTime(remindCommunicationAddDTO.getTime());
        entity.setFrequency(remindCommunicationAddDTO.getFrequency());
        entity.setTimes(remindCommunicationAddDTO.getTimes());
        entity.setCorpId(CorpContextHolder.getCorpId());
        int insert = this.baseMapper.insert(entity);
        return   insert> 0?R.ok("设置成功！"):R.failed("新增失败！");
    }

    @Override
    public R updateRule(RemindCommunicationUpdateDTO remindCommunicationUpdateDTO){
        RemindCommunicationEntity entity = new RemindCommunicationEntity();
        String tagListStr = JSONUtil.toJsonPrettyStr(remindCommunicationUpdateDTO.getTagIds());
        entity.setTagIds(tagListStr);
        entity.setId(remindCommunicationUpdateDTO.getId());
        entity.setTime(remindCommunicationUpdateDTO.getTime());
        entity.setFrequency(remindCommunicationUpdateDTO.getFrequency());
        entity.setTimes(remindCommunicationUpdateDTO.getTimes());
        entity.setCorpId(CorpContextHolder.getCorpId());
        return  this.update(entity, Wrappers.<RemindCommunicationEntity>lambdaQuery().eq(RemindCommunicationEntity::getId,remindCommunicationUpdateDTO.getId()))?R.ok("修改成功！"):R.failed("修改失败！");

    }

    @Override
    public R deleteRule(Integer id){
        return R.ok(removeById(id)? "删除成功":"删除失败");
    }

    @Override
    public R queryRecord() {
        List<RemindCommunicationEntity> list = this.list(Wrappers.<RemindCommunicationEntity>lambdaQuery().eq(RemindCommunicationEntity::getCorpId, CorpContextHolder.getCorpId()));
        QueryCommunicationVO vo = new QueryCommunicationVO();
        for (RemindCommunicationEntity entity : list) {
            List<Integer> listTag =JSONUtil.toList(JSONUtil.parseArray(entity.getTagIds()),Integer.class);
            vo.setId(entity.getId());
            vo.setTagIds(listTag);
            vo.setTime(entity.getTime());
            vo.setFrequency(entity.getFrequency());
            vo.setTimes(entity.getTimes());
            break;
        }
        return R.ok(vo);
    }


}
